<?php

	// +----------------------------------------------------------------------
	// | AppStore控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\admin;

	use app\common\event\Setting as SettingEvent;
	use think\facade\Cache;
	use think\facade\Config;
	use think\facade\Session;
	use app\event\admin\Version as VersionEvent;
	use yicmf\cloud\Server as ServerCloud;

	class Version extends Common
	{
		/**
		 * 在线商店服务
		 * @var ServerCloud
		 */
		protected $appstore;

		protected function initialize()
		{
			parent::initialize();
			$this->appstore = new ServerCloud();
		}

		public function index()
		{
			$system_config = [];
			$system_config['os'] = PHP_OS; // 服务器操作系统
			$system_config['server_software'] = $_SERVER['SERVER_SOFTWARE']; // 服务器环境
			$system_config['upload_max_filesize'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow'; // 文件上传限制
			$system_config['gd_version'] = gd_info()['GD Version']; // GD（图形处理）版本
			$system_config['max_execution_time'] = ini_get("max_execution_time") . "秒"; // 最大执行时间
			$system_config['port'] = $_SERVER['SERVER_PORT']; // 端口
			$system_config['dns'] = $_SERVER['HTTP_HOST']; // 服务器域名
			$system_config['php_version'] = PHP_VERSION; // php版本
			$system_config['ip'] = $_SERVER['SERVER_ADDR']; // 服务器
			$system_config['sockets'] = extension_loaded('sockets'); //是否支付sockets
			$system_config['openssl'] = extension_loaded('openssl'); //是否支付openssl
			$system_config['curl'] = function_exists('curl_init'); // 是否支持curl功能
			$system_config['fileinfo'] = function_exists('finfo_open'); // 是否支持curl功能
			$this->assign('system_config', $system_config);
			return $this->fetch();
		}

		public function online()
		{

			$data = $this->appstore->action('version.check');
			if (0 == $data['code']) {
				Session::set('version_next_to', $data['data']['version']);
				$this->assign('version', $data['data']['version']);
			}
			return $this->fetch();
		}


		/**
		 * 下载升级包
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		public function download()
		{
			if ($this->request->isPost()) {
				$updateEvent = new VersionEvent();
				$data = $updateEvent->download($this->request->post('step'));
				return json($data);
			} else {
				$version = Session::get('version_next_to');
				if (!$version) {
					$this->_returnError('未发现升级版本');
				}
				if ($version['for_build'] && Config::get('setting.build') != $version['for_build']) {
					$this->_returnError('更新包错误');
				}
				Session::set('nextVersion', $version);
				$data['code'] = 0;
				$data['data']['html'] = $this->fetch();
				return json($data);
			}
		}

		/**
		 * 比对代码
		 */
		public function compare()
		{
			$updateEvent = new VersionEvent();
			$result = $updateEvent->compare();
			if (false === $result) {
				$this->_returnError('对比代码异常。');
			} else {
				$this->assign('compare', $result);
				$data['code'] = 0;
				$data['data']['html'] = $this->fetch();
				return json($data);
			}
		}

		/**
		 * 覆盖代码
		 */
		public function cover()
		{
			if ($this->request->isPost()) {
				$updateEvent = new VersionEvent();
				return json($updateEvent->cover());
			} else {
				$data['code'] = 0;
				$data['data']['html'] = $this->fetch();
				return json($data);
			}
		}

		/**
		 * 升级数据库.
		 */
		public function updb()
		{
			$updateEvent = new VersionEvent();
			if ($this->request->isPost()) {
				$result = $updateEvent->upDb();
				return json($result);
			} else {
				$result = $updateEvent->getDbFilePath();
				if (false !== $result) {
					$this->assign('sql', file_get_contents($result));
				}
				$data['code'] = 0;
				$data['data']['html'] = $this->fetch();
				return json($data);
			}
		}

		/**
		 * 升级版本号.
		 */
		public function finish()
		{
			$updateEvent = new VersionEvent();
			$result = $updateEvent->finish();
			if (0 == $result['code']) {
				$this->assign('version', $result['data']);
				$data['code'] = 0;
				$data['data']['html'] = $this->fetch('finish');
				return json($data);
			} else {
				$this->_returnError($result['message']);
			}
		}


		/**
		 *  版本检查
		 */
		public function check()
		{
			$setting= config('setting');
			$data = $this->appstore->data($setting)->action('version.check');
			if (isset($data['data']['auth'])) {
				$setting['authorization'] = $data['data']['auth']['grant_type'];
				$setting['end_time'] = $data['data']['auth']['end_time'];
				$setting['app_id'] = $data['data']['auth']['app_id'];
				$setting['app_key'] = $data['data']['auth']['app_key'];
				SettingEvent::update($setting, 'setting');
			}
			if (isset($data['code']) && 0 == $data['code'] && isset($data['data']['version'])) {
				$result['code'] = 0;
				$result['message'] = '有更新';
				$result['data'] = 'v' . $data['data']['version']['version'] . '<sup>build' . $data['data']['version']['build'] . '</sup> <a lay-href="admin/Version/online" class="layui-badge layui-bg-red">在线升级</a>';
			} else {
				$result['code'] = 1;
				$result['message'] = '无更新';
			}
			return json($result);
		}
	}
